package webapp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.*;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

        /**
         * @see Servlet#init(ServletConfig)
         */
        public void init(ServletConfig config) throws ServletException {
                // TODO Auto-generated method stub
        }

        /**
         * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
         */
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                System.out.println("We are in Service method of servlet");
                
                String un=request.getParameter("username");
                String pw=request.getParameter("password");
                
                String msg=" ";
                boolean loginSuccess = false;
                
                Connection conn = null;
                String url = "jdbc:mysql://localhost:3306/";
                String dbName = "webappschema";
                String driver = "com.mysql.jdbc.Driver";
                String dbusername = "root";
                String dbpassword = "webapp";
                 
                try {
                      Class.forName(driver).newInstance();
                      conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
                      String strQuery = "select * from login where UserName='" + un + "' and password = '" + pw + "'" ;
                      Statement st = conn.createStatement();
                      ResultSet rs = st.executeQuery(strQuery);
                      
                      if(rs.next()){
                    	  if (((String)request.getParameter("username")).equals("admin") ) {
                    		  HttpSession session=request.getSession();  
                              session.setAttribute("name",un);
                        	  response.sendRedirect("AdminPanel.jsp");
                        	
                              }
                        
                    	  else  {
                    		  	HttpSession session=request.getSession();  
                    		  	session.setAttribute("name",un);
                        		response.sendRedirect("UserPanel.jsp"); 
                        		  
                                loginSuccess = true;
                                
                                return;
                        	   }
                         }
                      else
                         {
                          msg = "Incorrect username/password";
                         }
                         
                         rs.close();
                         st.close();
                         } catch (Exception e) {
                           e.printStackTrace();
                         }
                
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("<font size = '6' color=red>" + msg + "</font>");
                
                }
        }
